package xyz.sicau.test4_topic;

import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import xyz.sicau.utils.MQUtils;

import java.io.IOException;
import java.util.concurrent.TimeoutException;


/**
 * 主题模式，由一个交换机转发给多个队列
 */
public class Consumer {
    public static final String QUEUE = "que1";
    public static final String EXCHANGE = "MyJHJ";

    public static void main(String[] args) throws IOException, TimeoutException {
        Channel channel = MQUtils.getChannel();

        // 声明交换机和队列
        channel.exchangeDeclare(EXCHANGE, "topic");
        channel.queueDeclare(QUEUE,false,false,false,null);

        channel.queueBind(QUEUE,EXCHANGE,"*.orange.*");

        DeliverCallback deliverCallback = (consumerTag, message) -> {
            System.out.println(new String(message.getBody()) + "  绑定的键为："+message.getEnvelope().getRoutingKey());
        };

        CancelCallback cancelCallback = consumerTag -> {
            System.out.println("消息取消");
        };

        channel.basicConsume(QUEUE,true,deliverCallback,cancelCallback);
    }
}
